import 'dart:io';
import 'package:app_settings/app_settings.dart';
import 'package:device_info/device_info.dart';
import 'package:connectivity/connectivity.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

//网络状态，mobile移动流量，wifi，none未开通网络，noInternet网络开关开通未连上网络
enum NetStatus { mobile, wifi, none, noInternet }

class DeviceUtils {
  static bool isAndroid = Platform.isAndroid;
  static bool isIos = Platform.isIOS;
  //打开网络设置界面
  static openWIFISettings() => AppSettings.openWIFISettings();
  //打开安全设置界面
  static openSecuritySettings() => AppSettings.openSecuritySettings();
  //打开app设置界面
  static openAppSettings() => AppSettings.openAppSettings();
  //打开位置设置界面
  static openLocationSettings() => AppSettings.openLocationSettings();
  //获取设备信息
  static _DeviceInfo _deviceInfo;
  static Future<_DeviceInfo> get deviceInfo => _DeviceInfo._getInstance();
  //检测网络状态
  static Future<ConnectivityResult> checkConnectivity() =>_NetInfo().checkConnectivity();
  //设置状态栏颜色主题
  static setBarStatus(bool isDarkIcon, {Color color: Colors.transparent}) async{
    if (Platform.isAndroid) {
      if (isDarkIcon) {
        SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
            statusBarColor: color, statusBarIconBrightness: Brightness.dark);
        SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
      } else {
        SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
            statusBarColor: color, statusBarIconBrightness: Brightness.light);
        SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
      }
    }
  }
  //设置状态栏可见性
  static setBarVisible(bool visible, {Color color: Colors.transparent}) async{
    if (Platform.isAndroid) {
      if(visible){
        SystemChrome.setEnabledSystemUIOverlays([SystemUiOverlay.top]);
      }else{
        SystemChrome.setEnabledSystemUIOverlays([]);
      }
    }
  }
}

//设备信息类
class _DeviceInfo {
  static DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
  final IosDeviceInfo iosDeviceInfo;
  final AndroidDeviceInfo androidDeviceInfo;

  _DeviceInfo._internal(this.iosDeviceInfo, this.androidDeviceInfo);
  static _DeviceInfo _deviceInfo;
  static Future<_DeviceInfo> _getInstance() async {
    if (_deviceInfo == null) {
      AndroidDeviceInfo androidInfo;
      IosDeviceInfo iosDeviceInfo;
      if (Platform.isAndroid) {
        androidInfo = await deviceInfoPlugin.androidInfo;
      }
      if (Platform.isIOS) {
        iosDeviceInfo = await deviceInfoPlugin.iosInfo;
      }
      _deviceInfo = _DeviceInfo._internal(iosDeviceInfo, androidInfo);
    }
    return _deviceInfo;
  }

//  static Future<AndroidDeviceInfo> get androidInfo async=>await deviceInfoPlugin.androidInfo;
//  static Future<IosDeviceInfo> get iosInfo async=>await deviceInfoPlugin.iosInfo;
}

//网络连接状态
class _NetInfo {
  static NetStatus netStatus;
  static _NetInfo _info;
  final Connectivity connectivity;
  factory _NetInfo() => _getInstance();
  _NetInfo._internal(this.connectivity);

  Future<ConnectivityResult> checkConnectivity() async {
    return await (this.connectivity.checkConnectivity());
  }

  static _NetInfo _getInstance() {
    if (_info == null) {
      return _NetInfo._internal(Connectivity());
    }
    return _info;
  }
}
